//
// Created by tianq on 25-6-14.
//
#include <SFML/Graphics.hpp>

class DemoWnd {
    sf::RenderWindow window;
    sf::Font font;
    sf::Text text;
    int count = 0;

public:
    DemoWnd()
        : window(sf::VideoMode({800, 600}), "Click me buddy!"),
          font(R"(C:\Windows\Fonts\arial.ttf)"),
          text(font, "Hello there") {
    }

    void draw() {
        window.clear();
        window.draw(text);
        window.display();
    };

    void mapEvents(const std::optional<sf::Event> &event) {
        if (event->is<sf::Event::Closed>()) {
            window.close();
        }
        if (event->is<sf::Event::MouseMoved>()) {
            myFun(event->getIf<sf::Event::MouseMoved>()->position);
        }
        if (event->is<sf::Event::MouseButtonPressed>()) {
            myFun2();
        }
    };

    void run() {
        while (window.isOpen()) {
            draw();
            const std::optional<sf::Event> event = window.waitEvent();
            mapEvents(event);
        }
    }

    void myFun(const sf::Vector2i mousePos) {
        const sf::Vector2i textPos{mousePos.x + 15, mousePos.y + 20};
        text.setPosition(sf::Vector2f(textPos));
    }

    void myFun2() {
        count++;
        text.setString(std::to_string(count));
        window.setTitle("Click HARDER!!!");
    }
};

int main() {
    DemoWnd wnd;
    wnd.run();
}
